package com.matt.service.impl;

import com.matt.domain.Classes;
import com.matt.domain.course.Course;
import com.matt.domain.course.CoursePlanClasses;
import com.matt.domain.course.CourseTemplateAreaBuild;
import com.matt.domain.course.PrepareCourse;
import com.matt.foundation.DO.AdviseDetailDO;
import com.matt.foundation.DO.ChildrenCourseItemDO;
import com.matt.foundation.DO.TeacherDO;
import com.matt.foundation.DO.course.PadCourseTemplate3Vo;
import com.matt.foundation.constant.CourseNurseryConstant;
import com.matt.foundation.vo.course.PadCoursePlanDetailVo;
import com.matt.repository.ClassesRepository;
import com.matt.repository.CourseTemplate3Repository;
import com.matt.repository.base.ChildChildrenCourseItemRepository;
import com.matt.repository.base.ClassesChildrenRepository;
import com.matt.repository.course.CoursePlanClassesRepository;
import com.matt.repository.course.CourseRepository;
import com.matt.repository.course.PrepareCourseRepository;
import com.matt.service.CourseAreaBulidService;
import com.matt.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class CourseAreaBuildServiceImpl implements CourseAreaBulidService{

	@Autowired
	private CourseRepository courseRepository;

	@Autowired
	private CoursePlanClassesRepository coursePlanClassesRepository;

	@Autowired
	private ClassesChildrenRepository classesChildrenRepository;

	@Autowired
	private TeacherService teacherService;

	@Autowired
	private ClassesRepository classesRepository;

	@Autowired
	private CourseTemplate3Repository courseTemplate3Repository;

	@Autowired
	private ChildChildrenCourseItemRepository childChildrenCourseItemRepository;

	@Autowired
	PrepareCourseRepository prepareCourseRepository;
	@Override
	public PadCoursePlanDetailVo getAreaBuildCourseDetail(String coursePlanClassesId) {
		CoursePlanClasses coursePlanClasses = coursePlanClassesRepository.getOne(coursePlanClassesId);
		Course course = courseRepository.getOne(coursePlanClasses.getCourseId());

		List<AdviseDetailDO> adviseList = classesChildrenRepository.findAdviseByClasses(coursePlanClasses.getClassesId());//查询班级学生评测建议
		List<TeacherDO> teacherList = teacherService.getTeacherByClasses(coursePlanClasses.getClassesId());
		Classes classes = classesRepository.getOne(coursePlanClasses.getClassesId());

		Object template = null;

		CourseTemplateAreaBuild courseTemplate3 = courseTemplate3Repository.getOne(course.getCourseTemplateId());
		PrepareCourse prepareCourse = prepareCourseRepository.findByCoursePlanClassesId(coursePlanClasses.getId());

		if (Objects.nonNull(prepareCourse) && CourseNurseryConstant.PrepareStatus.PREPARED.codeOf() == coursePlanClasses.getPrepareStatus() || CourseNurseryConstant.PrepareStatus.APPROVED.codeOf() == coursePlanClasses.getPrepareStatus()) {
			//TODO 备课信息
//			PrepareTemplate3 prepareTemplate3 = prepareTemplate3Repository.findByPrepareCourseId(prepareCourse.getId());
//			template = PadCourseTemplate3Vo.ofPrepareCourse(prepareCourse, prepareTemplate3);
		} else {
			template = PadCourseTemplate3Vo.ofCourse(coursePlanClasses, courseTemplate3);
		}

		List<ChildrenCourseItemDO> itemList = childChildrenCourseItemRepository.findByChildrenByCoursePlanId(coursePlanClasses.getId());

		PadCoursePlanDetailVo vo = PadCoursePlanDetailVo.ofAreaBuild(course.getTemplateType(), coursePlanClasses, itemList, adviseList, teacherList, template, classes.getName());

		return vo;
	}

}
